package com.swj.common.core.utils.date;

import com.swj.common.core.exception.UtilException;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.Calendar;
import java.util.Date;


/**
 * 时间工具类
 *
 * @author swj
 */
public class DateUtil extends org.apache.commons.lang3.time.DateUtils {

    public static final String YYYY = "yyyy";

    public static final String YYYY_MM = "yyyy-MM";

    public static final String YYYY_MM_DD = "yyyy-MM-dd";

    public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static final String YYYY_MM_DD_HH_MM_SS_WITHWORD = "yyyy年MM月dd日 HH时mm分ss秒";

    private static final String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDateString() {
        return formatDateToStr(YYYY_MM_DD, new Date());
    }

    public static final String getTimeString() {
        return formatDateToStr(YYYY_MM_DD_HH_MM_SS, new Date());
    }

    /**
     * 日期转化为日期型字符串
     *
     * @param format 转换的格式
     * @param date   日期date
     * @return 转化后的字符串
     */
    public static final String formatDateToStr(final String format, final Date date) {
        return new SimpleDateFormat(format).format(date);
    }

    public static final String formatDateToStr(final String format) {
        return new SimpleDateFormat(format).format(new Date());
    }

    public static final String formatDateToStr(final Date date) {
        return new SimpleDateFormat(DateUtil.YYYY_MM_DD_HH_MM_SS).format(date);
    }

    /**
     * 日期型字符串转化为日期Date
     */
    public static Date parseStrToDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str.toString(), parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 日期型字符串转化为日期Date
     */
    public static Date parseStrToDate(Object str, String format) {
        if (str == null) {
            return null;
        }
        if (format == null) {
            format = YYYY_MM_DD;
        }
        try {
            return parseDate(str.toString(), format);
        } catch (ParseException e) {
            return null;
        }
    }

    public static final String dateTimeNow() {
        return formatDateToStr(YYYYMMDDHHMMSS, new Date());
    }

    public static final String dateTime(final Date date) {
        return DateFormatUtils.format(date, "yyyyMMdd");
    }

    public static final String date(final Date date) {
        return formatDateToStr(YYYY_MM_DD, date);
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算两个时间差
     */
    public static String getDateBetween(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    public static boolean isSameDay(Date date1, Date date2) {
        if (date1 != null && date2 != null) {
            Calendar cal1 = Calendar.getInstance();
            cal1.setTime(date1);
            Calendar cal2 = Calendar.getInstance();
            cal2.setTime(date2);
            return isSameDay(cal1, cal2);
        } else {
            throw new UtilException("DateUtil.isSameDay()方法的参数不能为空！");
        }
    }

    /**
     * 获取当前日期的前一天
     */
    public static Date getYesterday() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        return calendar.getTime();
    }

    /**
     * 获取当前日期，并指定时分秒
     *
     * @param hour   指定的小时
     * @param miunte 指定的分钟
     * @param second 指定的秒
     * @return 当天指定时分秒的日志
     */
    public static Date getDateTime(int hour, int miunte, int second) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, miunte);
        calendar.set(Calendar.SECOND, second);
        return calendar.getTime();
    }

    /**
     * 判断某个日期是否在【Start,end】时间区段内
     *
     * @param start 要比较的开始时间
     * @param end   要比较的结束时间
     * @param date  指定的日期
     * @return 在范围内，返回true，否则返回false
     */
    public static boolean isBetween(Date start, Date end, Date date) {
        if (date == null) {
            date = new Date();
        }
        return date.after(start) && date.before(end);
    }

    /**
     * 增加 LocalDateTime ==> Date
     */
    public static Date toDate(LocalDateTime temporalAccessor)
    {
        ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
        return Date.from(zdt.toInstant());
    }

    /**
     * 增加 LocalDate ==> Date
     */
    public static Date toDate(LocalDate temporalAccessor)
    {
        LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
        ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
        return Date.from(zdt.toInstant());
    }

}
